import type { TableColumns } from "@pureadmin/table";
import type { PlusColumn, PlusFormProps } from "plus-pro-components";

export enum FormPermission {
  Create = 1,
  Edit = 2,
  CreateEdit = Create | Edit
}

export const FormPermissionHas = (
  from: FormPermission,
  target: any
): boolean => {
  return `${from & target}` === `${target}`;
};

export type CrudFormColum = {
  search?: boolean; // 是否在搜索栏中显示
  edit?: boolean; //
  create?: boolean;
  editSort?: number; // 编辑表单排序
  createSort?: number; // 创建表单排序
  rules?: Record<string, any[]>; // 表单校验规则
  updateRules?: Record<string, any[]>; // 编辑表单校验规则
  createRules?: Record<string, any[]>; // 创建表单校验规则
};

export type Column = TableColumns & PlusColumn & CrudFormColum;

export type ModelFormOpts = PlusFormProps & {
  viewType?: "dialog" | "drawer"; // 编辑表单弹窗类型 (默认: dialog)
  labelPosition?: string;
  title?: string; // 编辑表单弹窗标题
  columns?: Column[]; // 编辑表单字段
};

export type PageOptions = {
  title: string; // 页面标题
  columns?: Column[]; // 页面内实例字段
  modelValue?: any[]; // 页面内实例数据

  rowKey?: string; // 表格行 key
  align?: string; // 表格对齐方式
  size?: string; // 表格尺寸
  bordered?: boolean; // 是否显示边框
  defaultExpandAll?: boolean; // 是否默认展开所有行
  headerCellStyle?: any; // 表头样式
  childrenFieldName?: string; // 子表格字段名 (默认: children)

  edit?: ModelFormOpts; // 编辑表单弹窗配置
  create?: ModelFormOpts; // 创建表单弹窗配置

  // events
  getDataList?: (
    params: any & { pageNum?: number; pageSize?: number }
  ) => Promise<{ total?: number; list?: any[] }>; // 获取数据列表
};
